/*
 * Copyright 2006-2023 ahtelit.com, All Rights Reserved.
 */

package com.telit.firecommand.controller;

import com.telit.common.constant.Constant;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.firecommand.dto.DistrictCountDTO;
import com.telit.firecommand.entity.IotData;
import com.telit.firecommand.entity.IotDevice;
import com.telit.firecommand.model.iot.IotDeviceListModel;
import com.telit.firecommand.model.iot.QryDevicesDataRequest;
import com.telit.firecommand.model.iot.QryDevicesListRequest;
import com.telit.firecommand.service.IotService;
import com.telit.firecommand.service.impl.MqttClientServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 升哲物联网API
 *
 * @author yuezhuang
 * @since 2023-04-27
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/iot", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "升哲物联网API", value = "升哲物联网API", produces = MediaType.APPLICATION_JSON_VALUE)
public class IotController {

    private final IotService iotService;

    private final MqttClientServiceImpl mqttClientService;

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "查询第三方IOT设备列表", hidden = true)
    @GetMapping(path = "/qry-out-device-list")
    public DataReturn<PageData<List<IotDeviceListModel>>> qryOutDeviceList(
            QryDevicesListRequest qryDevicesListRequest) {
        return DataReturn.ok(iotService.qryOutDeviceList(qryDevicesListRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "查询IOT设备列表")
    @GetMapping(path = "/qry-device-list")
    public DataReturn<PageData<List<IotDevice>>> qryDeviceList(QryDevicesListRequest qryDevicesListRequest) {
        return DataReturn.ok(iotService.qryDeviceList(qryDevicesListRequest));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id，和sn两者必填一个，优先级高于sn", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sn", value = "设备SN，和id两者必填一个", paramType = "query", dataType = "String"),
    })
    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "查询IOT设备详情")
    @GetMapping(path = "/qry-device")
    public DataReturn<IotDevice> qryDeviceDetail(@RequestParam(value = "id", required = false) Long id,
                                                 @RequestParam(value = "sn", required = false) String sn) {
        return DataReturn.ok(iotService.qryDeviceDetail(id, sn));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "查询IOT设备数据列表")
    @GetMapping(path = "/qry-device-data-list")
    public DataReturn<PageData<List<IotData>>> qryDeviceDataList(QryDevicesDataRequest qryDevicesDataRequest) {
        return DataReturn.ok(iotService.qryDeviceDataList(qryDevicesDataRequest));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "networkStatus", value = "状态：true-在线 false-离线", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "districtCode", value = "行政区划编码", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/count", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询数量", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<DistrictCountDTO> count(@RequestParam(required = false) String name,
                                              @RequestParam(required = false) Boolean networkStatus,
                                              @RequestParam(value = "districtCode") String districtCode) {
        return DataReturn.ok(iotService.count(districtCode, networkStatus, name));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "networkStatus", value = "状态：true-在线 false-离线", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "districtCode", value = "行政区划编码", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/tree-district", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询树形结构", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<DistrictCountDTO> treeDistrict(@RequestParam(required = false) String name,
                                                     @RequestParam(required = false) Boolean networkStatus,
                                                     @RequestParam(value = "districtCode") String districtCode) {
        return DataReturn.ok(iotService.treeDistrict(districtCode, networkStatus, name));
    }

}
